#include "space\helpers\file.h"

namespace space {
	namespace helpers {
		namespace file {
			result list_files(std::vector<std::string> & return_list, char *directory, char *extension = "*.*") {
#ifdef _WIN32
				bool working(true);
				string buffer, searchPath, filename;
				result res = RESULT_OK;

				searchPath = directory + string("\\") + extension;

				WIN32_FIND_DATAA findData;
				HANDLE myHandle=FindFirstFileA(searchPath.c_str(), &findData);

				if(myHandle!=INVALID_HANDLE_VALUE) {
					filename = findData.cFileName;
					buffer = directory + string("\\") + findData.cFileName;
					return_list.push_back(buffer);

					while(working) {
						FindNextFileA(myHandle, &findData);
						if(findData.cFileName != filename) {
							filename = findData.cFileName;
							buffer = directory + string("\\") + findData.cFileName;
							return_list.push_back(buffer);
						} else {
							//end of files reached
							working = false;
						}
					}
				} else {
					res = RESULT_ERROR;
					res.value = ERROR_FILE_NOT_FOUND;
					res.extended_value = "Directory does not exist.";
				}
#endif
				return res;
			}
		};
	};
};